/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.statistics.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.z100h.supplychain.statistics.entity.StatRealtimeResource;
import com.z100h.supplychain.statistics.service.StatRealtimeResourceService;

/**
 * 成品库资源Controller
 * @author fyh
 * @version 2019-02-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sc/realtimeresource/statRealtimeResource")
public class StatRealtimeResourceController extends BaseController {

	@Autowired
	private StatRealtimeResourceService statRealtimeResourceService;

	@ModelAttribute
	public StatRealtimeResource get(@RequestParam(required = false) String id) {
		StatRealtimeResource entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = statRealtimeResourceService.get(id);
		}
		if (entity == null) {
			entity = new StatRealtimeResource();
		}
		return entity;
	}

	/**
	 * 成品库资源列表页面
	 */
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:list")
	@RequestMapping(value = { "list", "" })
	public String list() {
		return "supplychain/statistics/statRealtimeResourceList";
	}

	/**
	* 成品库资源列表数据
	*/
	@ResponseBody
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(StatRealtimeResource statRealtimeResource, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<StatRealtimeResource> page = statRealtimeResourceService
				.findPage(new Page<StatRealtimeResource>(request, response), statRealtimeResource);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑成品库资源表单页面
	 */
	@RequiresPermissions(value = { "sc:realtimeresource:statRealtimeResource:view",
			"sc:realtimeresource:statRealtimeResource:add",
			"sc:realtimeresource:statRealtimeResource:edit" }, logical = Logical.OR)
	@RequestMapping(value = "form")
	public String form(StatRealtimeResource statRealtimeResource, Model model) {
		model.addAttribute("statRealtimeResource", statRealtimeResource);
		if (StringUtils.isBlank(statRealtimeResource.getId())) {// 如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/sc/realtimeresource/statRealtimeResourceForm";
	}

	/**
	 * 保存成品库资源
	 */
	@RequiresPermissions(value = { "sc:realtimeresource:statRealtimeResource:add",
			"sc:realtimeresource:statRealtimeResource:edit" }, logical = Logical.OR)
	@RequestMapping(value = "save")
	public String save(StatRealtimeResource statRealtimeResource, Model model, RedirectAttributes redirectAttributes)
			throws Exception {
		if (!beanValidator(model, statRealtimeResource)) {
			return form(statRealtimeResource, model);
		}
		// 新增或编辑表单保存
		statRealtimeResourceService.save(statRealtimeResource);// 保存
		addMessage(redirectAttributes, "保存成品库资源成功");
		return "redirect:" + Global.getAdminPath() + "/sc/realtimeresource/statRealtimeResource/?repage";
	}

	/**
	 * 删除成品库资源
	 */
	@ResponseBody
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(StatRealtimeResource statRealtimeResource, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		statRealtimeResourceService.delete(statRealtimeResource);
		j.setMsg("删除成品库资源成功");
		return j;
	}

	/**
	 * 批量删除成品库资源
	 */
	@ResponseBody
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] = ids.split(",");
		for (String id : idArray) {
			statRealtimeResourceService.delete(statRealtimeResourceService.get(id));
		}
		j.setMsg("删除成品库资源成功");
		return j;
	}

	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:export")
	@RequestMapping(value = "export", method = RequestMethod.POST)
	public AjaxJson exportFile(StatRealtimeResource statRealtimeResource, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			String fileName = "成品库资源" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<StatRealtimeResource> page = statRealtimeResourceService
					.findPage(new Page<StatRealtimeResource>(request, response, -1), statRealtimeResource);
			new ExportExcel("成品库资源", StatRealtimeResource.class).setDataList(page.getList()).write(response, fileName)
					.dispose();
			j.setSuccess(true);
			j.setMsg("导出成功！");
			return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出成品库资源记录失败！失败信息：" + e.getMessage());
		}
		return j;
	}

	/**
	 * 导入Excel数据
	
	 */
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:import")
	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<StatRealtimeResource> list = ei.getDataList(StatRealtimeResource.class);
			for (StatRealtimeResource statRealtimeResource : list) {
				try {
					statRealtimeResourceService.save(statRealtimeResource);
					successNum++;
				} catch (ConstraintViolationException ex) {
					failureNum++;
				} catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum > 0) {
				failureMsg.insert(0, "，失败 " + failureNum + " 条成品库资源记录。");
			}
			addMessage(redirectAttributes, "已成功导入 " + successNum + " 条成品库资源记录" + failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入成品库资源失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/sc/realtimeresource/statRealtimeResource/?repage";
	}

	/**
	 * 下载导入成品库资源数据模板
	 */
	@RequiresPermissions("sc:realtimeresource:statRealtimeResource:import")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "成品库资源数据导入模板.xlsx";
			List<StatRealtimeResource> list = Lists.newArrayList();
			new ExportExcel("成品库资源数据", StatRealtimeResource.class, 1).setDataList(list).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/sc/realtimeresource/statRealtimeResource/?repage";
	}

}